/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.articles.importFromLn.articlesData;

import cz.mzk.kramerius.articles.importFromLn.articlesData.errors.ArticleError;
import cz.mzk.kramerius.articles.importFromLn.articlesData.errors.EmptyFieldError;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Martin Řehánek (rehan@mzk.cz)
 */
public class ArticleStatistics {

    //map for counting errors (except for EmptyFieldError).
    //if article contains several errors, there will be multiple put() calls to this map
    private final Map<String, Integer> errorCounter = new HashMap<String, Integer>();
    private final Map<String, Integer> emptyFieldCounter = new HashMap<String, Integer>();
    private int correctArticleCounter = 0;
    private int articleWithErrorsCounter = 0;
    private int articleCounter = 0;

    /**
     * This method is called once for each article being build
     *
     * @param errors
     */
    public void updateCounters(List<ArticleError> errors) {
        articleCounter++;
        if (errors.isEmpty()) {
            correctArticleCounter++;
        } else {
            articleWithErrorsCounter++;
            for (ArticleError error : errors) {
                updateCounter(error);
            }
        }
    }

    private void updateCounter(ArticleError error) {
        if (error instanceof EmptyFieldError) {
            updateEmptyFieldCounter((EmptyFieldError) error);
        } else {
            updateErrorCounter(error);
        }
    }

    private void updateErrorCounter(ArticleError error) {
        String className = error.getClass().getCanonicalName();
        Integer counter = errorCounter.get(className);
        if (counter == null) {
            counter = Integer.valueOf(0);
        }

        Integer newCounter = Integer.valueOf(counter.intValue() + 1);
        errorCounter.put(className, newCounter);
    }

    private void updateEmptyFieldCounter(EmptyFieldError error) {
        String field = error.getField();
        Integer counter = emptyFieldCounter.get(field);
        if (counter == null) {
            counter = Integer.valueOf(0);
        }
        Integer newCounter = Integer.valueOf(counter.intValue() + 1);
        emptyFieldCounter.put(field, newCounter);
    }

    private int getTotalEmptyFields() {
        int result = 0;
        for (String key : emptyFieldCounter.keySet()) {
            result += emptyFieldCounter.get(key).intValue();
        }
        return result;
    }

    public void print(PrintStream out) {
        out.println("Total articles: " + articleCounter);
        out.println("Correct articles: " + correctArticleCounter);
        out.println("Article with errors: " + articleWithErrorsCounter);
        for (String className : errorCounter.keySet()) {
            out.println(className + ": " + errorCounter.get(className));
        }
        if (!emptyFieldCounter.isEmpty()) {
            out.println(EmptyFieldError.class.getCanonicalName() + ": " + getTotalEmptyFields() + " (details follow)");
            for (String field : emptyFieldCounter.keySet()) {
                out.println(field + ": " + emptyFieldCounter.get(field));
            }
        }
    }
}
